package com.pac.questionnaire.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.common.tools.StringTools;
import com.common.util.ErrorNumber;
import com.common.util.LogicException;
import com.pac.questionnaire.bean.QuestionnaireAnswerBean;
import com.pac.questionnaire.dao.QuestionnaireAnswerDao;
import com.pac.questionnaire.service.QuestionnaireAnswerService;

/**
 * 答卷答案Service实现
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月9日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service(value = "questionnaireAnswerService")
public class QuestionnaireAnswerServiceImpl implements
		QuestionnaireAnswerService {

	private static Logger logger = Logger
			.getLogger(QuestionnaireAnswerServiceImpl.class);

	@Autowired
	private QuestionnaireAnswerDao questionnaireAnswerDao;

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public String addQuestionnaireAnswer(
			QuestionnaireAnswerBean questionnaireAnswerBean) {
		if (null == questionnaireAnswerBean) {
			logger.error("添加答卷答案出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(questionnaireAnswerBean.getQuestionnaireId())) {
			logger.error("添加答卷答案出错，问卷ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问卷ID为空");
		}

		if (StringTools.isNull(questionnaireAnswerBean.getQuestionId())) {
			logger.error("添加答卷答案出错，问题ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "问题ID为空");
		}

		return questionnaireAnswerDao
				.addQuestionnaireAnswer(questionnaireAnswerBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean updateQuestionnaireAnswer(
			QuestionnaireAnswerBean questionnaireAnswerBean) {
		if (null == questionnaireAnswerBean) {
			logger.error("修改答卷答案出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(questionnaireAnswerBean.getId())) {
			logger.error("修改答卷答案出错，答卷ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "答卷ID为");
		}

		return questionnaireAnswerDao
				.updateQuestionnaireAnswer(questionnaireAnswerBean);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean delQuestionnaireAnswer(
			QuestionnaireAnswerBean questionnaireAnswerBean) {
		if (null == questionnaireAnswerBean) {
			logger.error("删除答卷答案出错，参数为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "参数为空");
		}

		if (StringTools.isNull(questionnaireAnswerBean.getId())) {
			logger.error("删除答卷答案出错，答卷ID为空");
			throw new LogicException(ErrorNumber.PARAM_IS_NULL, "答卷ID为空");
		}

		return questionnaireAnswerDao
				.delQuestionnaireAnswer(questionnaireAnswerBean);
	}

	@Override
	public List<QuestionnaireAnswerBean> queryQuestionnaireAnswer(
			QuestionnaireAnswerBean questionnaireAnswerBean) {
		return questionnaireAnswerDao
				.queryQuestionnaireAnswer(questionnaireAnswerBean);
	}

	@Override
	public QuestionnaireAnswerBean findQuestionnaireAnswer(
			QuestionnaireAnswerBean questionnaireAnswerBean) {
		
		if (null == questionnaireAnswerBean) {
			return null;
		}

		List<QuestionnaireAnswerBean> questionnaires = questionnaireAnswerDao
				.queryQuestionnaireAnswer(questionnaireAnswerBean);
		if (null != questionnaires && questionnaires.size() > 0) {
			return questionnaires.get(0);
		}

		return null;
	}

}
